package utils;


import java.util.logging.Logger;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * Utility class for Hibernate that provides the Hibernate implementation of the
 * session factory.
 * 
 * @author adam
 * 
 */
public final class HibernateUtil {

	/**
	 * The logger object.
	 */
	private static final Logger LOG = Logger.getLogger("hibernate_util");

	/**
	 * The Hibernate implementaion of the session factory, provides the session.
	 */
	private static final SessionFactory SESSION_FACTORY;

	/**
	 * Disallowing the instatiation.
	 */
	private HibernateUtil() {
	}

	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			SESSION_FACTORY = new Configuration().configure().buildSessionFactory();
		} catch (HibernateException ex) {
			// Make sure you log the exception, as it might be swallowed
			LOG.severe("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	/**
	 * Provices the session factory.
	 * 
	 * @return the session factory
	 */
	public static SessionFactory getSessionFactory() {
		return SESSION_FACTORY;
	}

}